#include "sspch.h"
#include "Painter.h"

#include "utils/singleton.h"

int main() {
	const sf::Vector2i size(400, 800);

	sf::RenderWindow window(sf::VideoMode(size.x, size.y), "SandSim", sf::Style::Close);

	SandMap map(size);
	Painter painter(map);

	window.setFramerateLimit(120);

	float updatetime = 0.0f;

	while (window.isOpen()) {
		sf::Event ev;
		while (window.pollEvent(ev)) {
			if (ev.type == sf::Event::Closed) {
				window.close();
			}
			if (ev.type == sf::Event::MouseWheelScrolled &&
				sf::Keyboard::isKeyPressed(sf::Keyboard::LControl)) {
				painter.scale_brush(ev.mouseWheelScroll.delta);
			}
		}
		if (sf::Keyboard::isKeyPressed(sf::Keyboard::E))
			painter.set_brush(DrawType::Erase);
		if (sf::Keyboard::isKeyPressed(sf::Keyboard::S))
			painter.set_brush(DrawType::Sandd);
		if (sf::Keyboard::isKeyPressed(sf::Keyboard::R))
			painter.set_brush(DrawType::Rockk);
		if (sf::Keyboard::isKeyPressed(sf::Keyboard::W))
			painter.set_brush(DrawType::Waterr);

		// update
		painter.update(sf::Mouse::getPosition(window),
			sf::Mouse::isButtonPressed(sf::Mouse::Left));

		if (std::clock() - updatetime > 5) {
			map.update();
			updatetime = std::clock();
		}

		// render
		window.clear(sf::Color::Black);
		window.draw(map);
		window.draw(painter);
		window.display();

	}

}
